Skip to content

Add zipWith to NonEmptyList and NonEmptyVector - #1885

Merged
kailuowang merged 1 commit into
typelevel:masterfrom
LukaJCB:zip-with
Sep 1, 2017
Merged

Add zipWith to NonEmptyList and NonEmptyVector#1885
kailuowang merged 1 commit into
typelevel:masterfrom
LukaJCB:zip-with

Conversation

@LukaJCB

@LukaJCB LukaJCB commented Sep 1, 2017

Copy link
Copy Markdown
Member

Should resolve #1870

@codecov-io

codecov-io commented Sep 1, 2017

Copy link
Copy Markdown

Codecov Report

Merging #1885 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1885      +/-   ##
==========================================
+ Coverage   94.97%   94.97%   +<.01%     
==========================================
  Files         241      241              
  Lines        4199     4202       +3     
  Branches      106      112       +6     
==========================================
+ Hits         3988     3991       +3     
  Misses        211      211
Impacted Files Coverage Δ
core/src/main/scala/cats/data/NonEmptyList.scala 100% <100%> (ø) ⬆️
core/src/main/scala/cats/data/NonEmptyVector.scala 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d85cc86...5abdb48. Read the comment docs.

* }}}
*/
def zipWith[B, C](b: NonEmptyVector[B])(f: (A, B) => C): NonEmptyVector[C] =
NonEmptyVector.fromVectorUnsafe((toVector, b.toVector).zipped.map(f))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will be maybe faster if we do:

toVector.iterator.zip(b.toVector.iterator).map(f).toVector

I think the current code has to allocate a NonEmptyVector after zipped and then another one after map.

But I'm just guessing since I don't know this tuple enrichment. Can you comment as to why this is incorrect if I am wrong?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes of course! I'm on my phone right now, but if you look at the source for zipped https://github.com/scala/scala/tree/v2.12.3/src/library/scala/runtime/Tuple2Zipped.scala#L1 you'll see that it does not actually iterate over either of the vectors until you call map or any of the other methods :)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahh nice.

@johnynek

johnynek commented Sep 1, 2017

Copy link
Copy Markdown
Contributor

👍

@kailuowang
kailuowang merged commit e8891b1 into typelevel:master Sep 1, 2017
@LukaJCB
LukaJCB deleted the zip-with branch September 1, 2017 22:50
@LukaJCB LukaJCB mentioned this pull request Sep 2, 2017
@kailuowang kailuowang added this to the 1.0.0-RC1 milestone Oct 13, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add zipWith to NonEmptyList

4 participants